# -*- perl -*-

use Test::More tests => 6;
use HTML::FormFu;
use lib 't/lib';
use DBSchema;
use YAML::Syck qw( Load );

my $dsn    = 'dbi:SQLite:dbname=t/var/dvdzbr.db';
my $schema = DBSchema->connect( $dsn, '', '', {} );
$schema->deploy({ add_drop_table => 1, });
$schema->populate('User', [
    [ qw/id username name password / ],
    [ 1, 'jgda', 'Jonas Alves', ''],
    [ 2, 'isa' , 'Isa', '', ],
    [ 3, 'zby' , 'Zbyszek Lukasiak', ''],
    ]
);
$schema->populate('Tag', [
    [ qw/id name file / ],
    [ 1, 'comedy', '' ],
    ]
);
$schema->populate('Dvd', [
    [ qw/id name imdb_id owner current_borrower creation_date alter_date / ],
    [ 1, 'Picnick under the Hanging Rock', 123, 1, 1, '', '' ],
    [ 2, 'The Deerhunter', 1234, 1, 1, '', '' ],
    [ 3, 'Rejs', 1235, 1, 1, '', '' ],
    [ 4, 'Seksmisja', 1236, 1, 1, '', '' ],
    ]
); 
$schema->populate( 'Dvdtag', [
    [ qw/ dvd tag / ],
    [ 3, 1 ],
    [ 4, 1 ],
    ]
);

my $form = HTML::FormFu->new( { stash => { schema => $schema } } );
$form->populate( Load( do { local $/; <DATA> } ) );
my $by_pkey = $form->get_element( { name => 'user' } );
my $options = $by_pkey->_options;
is ( scalar @$options, 3, 'Number of options' );
is ( $options->[0]->{label}, 1, 'First option label' );
is ( $options->[0]->{value}, 1, 'First option value' );
$options = $form->get_element( { name => 'name' } )->_options;
is ( $options->[0]->{label}, 'Jonas Alves', 'First option label 2' );

$options = $form->get_element( { name => 'where' } )->_options;
is ( scalar @$options, 1, 'Number of options - where' );
is ( $options->[0]->{label}, 'Zbyszek Lukasiak', 'First option label - where' );


#warn Dumper( $by_pkey->_options ); use Data::Dumper;

__DATA__

---
elements:
   - type: Select::DBIC
     name: user
     dbic_args: 
       resultset_name: User
       search_attr: 
         order_by: 
           - id
   - type: Select::DBIC
     name: name 
     dbic_args: 
       resultset_name: User
       presentation: name
       search_attr: 
         order_by: 
           - id
   - type: Select::DBIC
     name: where 
     dbic_args: 
       resultset_name: User
       presentation: name
       search_attr: 
         where: 
           id: 3
         order_by: 
           - id
  

